/**
 * @class common.pagination
 * @extends common.base.object
 * 分页组件
 */
define(function (require) {
    var BaseObject = require('common/base/object'),
        PageTpl = require('text!common/templates/pagination.html');

    return BaseObject.extend({
        initialize: function (params) {
            this.$wrap = _.isString(params.wrap) ? $(params.wrap) : params.wrap;
            this.params = params;
            this.id = 'pagination_' + _.time() + '_' + _.rand(1000, 9999);
            this.el = '#' + this.id;
        },
        /*
         *传递参数格式：{p:1,count:100,pageSize:15,baseUrl:'index.php?g=admin&....'}
         * */
        show: function (data, callback, context) {
            var totalPage = parseInt(data.count / data.pageSize),
                _this = this;
            _.defaults(data, {baseUrl: '#'});
            if (data.count % data.pageSize > 0) {
                totalPage++;
            }
            this.$wrap.show();
            data = _.extend(data, {
                totalPage: totalPage,
                id: _this.id,
                style: _.empty(_this.params.style) ? 'right' : _this.params.style
            });
            this.$wrap.html(this.render(PageTpl, data));
            if (callback) {
                _this.$('a').click(function () {
                    if (!$(this).parents('li').hasClass('active')) {
                        callback.apply(context, [_.parse_hash($(this).attr('href'))]);
                    }
                    return false;
                });
            }
            $('a[href="' + data.baseUrl + '&p=' + data.p + '"]', this.$wrap).parent().addClass('active');
        },
        hide: function () {
            this.$wrap.hide();
        }
    });
});